<template>
  <div class="login-container">
    <h2>登录</h2>
    <form @submit.prevent="handleLogin">
      <div class="input-group">
        <label for="username">用户名</label>
        <input v-model="username" id="username" type="text" required />
      </div>
      <div class="input-group">
        <label for="password">密码</label>
        <input v-model="password" id="password" type="password" required />
      </div>
      <button type="submit">登录</button>
      <p>还没有账号？<router-link to="/register">注册</router-link></p>
    </form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    async handleLogin() {
      try {
        const response = await axios.post('http://192.168.1.69:8888/v1/user/login', {
          username: this.username,
          password: this.password
        });

        // 处理响应
        if (response.status === 200) {
          if (response.data.code == 200) {
            alert('登录成功');
          } else {
            alert(response.data.msg);
          }
          
        } else {
          alert('登录失败');
        }
      } catch (error) {
        console.error('请求失败：', error);
        alert('请求失败，请稍后再试');
      }
    }
  }
};
</script>

<style scoped>

.login-container {
  max-width: 400px;
  margin: auto;
  padding: 20px;
  background-color: #f9f9f9;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.input-group {
  margin-bottom: 15px;
}
input {
  width: 100%;
  padding: 8px;
  margin-top: 5px;
}
button {
  width: 100%;
  padding: 10px;
  background-color: #4caf50;
  color: white;
  border: none;
  cursor: pointer;
}
button:hover {
  background-color: #45a049;
}
</style>
